package com.edz.zxpmifiapk;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class TestService extends Service {

    private static final String TAG = "TestService";

    public TestService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * 第一次创建服务的时候调用
     */
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("TestService","onCreate()-----------------------子线程中发送测试数据到服务器");
        new Thread(new Runnable() {
            @Override
            public void run() {
                SocketUtil.getInstance().sendData2Server("client send data to server");
            }
        }).start();
    }

    /**
     * 每次启动服务都会调用
     * @param intent
     * @param flags
     * @param startId
     * @return
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("TestService","onStartCommand()");
        new Thread(new Runnable() {
            @Override
            public void run() {
//                String s = SocketUtil.getInstance().Server2Client();
//                Log.e("test data",s);
                SocketUtil.getInstance().sendHeartData("heart data 2s after");
            }
        }).start();

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        new Thread(new Runnable() {
            @Override
            public void run() {
                SocketUtil.getInstance().disConnectServer();
            }
        }).start();
        Log.e("TestService","onDestroy()");
    }
}
